package main

import (
	"encoding/json"
	"fmt"
	"time"
)

//按key查出val
func get(key *string) (*string, error) {
	res := client.Get(*key)
	val, err := res.Result()
	if err != nil {
		fmt.Printf("查询key%s出错 %v\n", *key, err)
		return nil, err
	}
	return &val, nil
}

//按key追加到原来的val后 并返回长度
func append(key *string, val *string) (int64, error) {
	res := client.Append(*key, *val)
	if res.Err() != nil {
		fmt.Printf("存入key%s出错 %v\n", *key, res.Err())
		return 0, res.Err()
	}
	return res.Val(), nil
}

//set 方法 按过期时间秒存入
func set(key *string, val *string, expiration time.Duration) (string, error) {
	res := client.Set(*key, *val, expiration)
	if res.Err() != nil {
		fmt.Printf("存入key%s出错 %v\n", *key, res.Err())
		return "nil", res.Err()
	}
	return res.Val(), nil
}

//set 方法 按结构体存入
func setStruct(key *string, val User, expiration time.Duration) (string, error) {
	js, err := json.Marshal(val)
	if err != nil {
		fmt.Printf("json转换出错了 %v\n", err)
		return "", err
	}
	res := client.Set(*key, js, expiration)
	if res.Err() != nil {
		fmt.Printf("存入key%s出错 %v\n", *key, res.Err())
		return "nil", res.Err()
	}
	return res.Val(), nil
}
